﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreHelper.Validation.Validators
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class MinLengthValidatorAttribute : BaseValidatorAttribute
    {
        public MinLengthValidatorAttribute(int length) : this(length, ValidationUtils.Messages.Minlength)
        {
        }

        public MinLengthValidatorAttribute(int length, string errorMessage) : base(errorMessage)
        {
            Length = length;
        }

        public int Length { get; protected set; }

        public override bool IsValid(object value)
        {
            if (value == null) return false;
            var len = ValidationUtils.GetValueLenth(value);
            if (len == null) return true;
            return len >= Length;
        }

        public override string FormatErrorMessage()
        {
            return string.Format(ErrorMessage, Length);
        }
    }
}
